package api

import (
	"github.com/astaxie/beego"
	. "magiciterm/models"
	"strconv"
)

type PostController struct {
	beego.Controller
}

func (p *PostController) AddPost() {
	var (
		num  int64
		err  error
		post Post
	)

	post = Post{
		Post_title:    p.GetString("post_title"),
		Post_img:      p.GetString("post_title"),
		Post_tags:     p.GetString("post_title"),
		Post_desc:     p.GetString("post_title"),
		Post_type:     p.GetString("post_title"),
		Post_text:     p.GetString("post_title"),
		Post_author:   p.GetString("post_title"),
		Post_urlname:  p.GetString("post_title"),
		Post_datetime: p.GetString("post_title"),
		Post_category: p.GetString("post_title"),
	}

	num, err = AddPost(post)
	if nil != err {
		p.Data["json"] = map[string]interface{}{"result": false, "msg": "error info - " + err.Error()}
		p.ServeJson()
	} else if num == 0 {
		p.Data["json"] = map[string]interface{}{"result": false, "msg": "post exist"}
		p.ServeJson()
	} else {
		p.Data["json"] = map[string]interface{}{"result": true, "msg": "post added"}
		p.ServeJson()
	}
}

func (p *PostController) AddPostTest() {
	var (
		num  int64
		err  error
		post Post
	)
	for i := 1; i < 15; i++ {

		var _i = strconv.Itoa(i)

		post = Post{
			Post_title:    "MT",
			Post_img:      "http://grimrock.qiniudn.com/v1/images/gallery/" + _i + ".jpg?imageView2/1/w/570/h/570",
			Post_tags:     "tags",
			Post_desc:     "MT Design",
			Post_type:     "image",
			Post_text:     "http://grimrock.qiniudn.com/v1/images/gallery/" + _i + ".jpg?imageView2/1/w/570/h/570",
			Post_author:   "MT",
			Post_urlname:  "test" + _i,
			Post_datetime: "2014/09/03-02:09:37pm",
			Post_category: "MT CT",
		}

		num, err = AddPost(post)
		if nil != err {
			p.Ctx.WriteString(err.Error() + "\n")
		} else if num == 0 {
			p.Ctx.WriteString(err.Error() + "\n")
		} else {
			p.Ctx.WriteString("Success \n")
		}
	}
}

func (p *PostController) GetPosts() {

	maps, err := GetPosts()

	if nil != err {
		p.Ctx.WriteString(err.Error())
	}
	p.Data["json"] = map[string]interface{}{"result": true, "posts": maps}
	p.ServeJson()
}

func (p *PostController) GetPost() {
	var (
		post Post
		err  error
	)

	_urlname := p.Ctx.Input.Param(":urlname")

	if _urlname != "" {

		post, err = GetPostByName(_urlname)
		if nil == err {
			p.Data["json"] = post
			p.ServeJson()
		} else {
			p.Ctx.WriteString(err.Error())
		}

	} else {

		_pid, _ := strconv.Atoi(p.Ctx.Input.Param(":pid"))

		post, err = GetPostById(int(_pid))
		if nil == err {
			p.Data["json"] = post
			p.ServeJson()
		} else {
			p.Ctx.WriteString(err.Error())
		}
	}

}

func (p *PostController) DelPost() {
	var (
		err error
		num int64
	)

	_urlname := p.Ctx.Input.Param(":urlname")

	if _urlname != "" {

		num, err = DelPostByName(_urlname)

		if nil != err {
			p.Data["json"] = map[string]interface{}{"result": false, "msg": "error info - " + err.Error()}
			p.ServeJson()
		} else if 0 == num {
			p.Data["json"] = map[string]interface{}{"result": false, "msg": "post dose not exist"}
			p.ServeJson()
		} else {
			p.Data["json"] = map[string]interface{}{"result": true, "msg": "post deleted"}
			p.ServeJson()
		}

	} else {

		_pid, _ := strconv.Atoi(p.Ctx.Input.Param(":pid"))

		num, err = DelPostById(int(_pid))

		if nil != err {
			p.Data["json"] = map[string]interface{}{"result": false, "msg": "error info - " + err.Error()}
			p.ServeJson()
		} else if 0 == num {
			p.Data["json"] = map[string]interface{}{"result": false, "msg": "post dose not exist"}
			p.ServeJson()
		} else {
			p.Data["json"] = map[string]interface{}{"result": true, "msg": "post deleted"}
			p.ServeJson()
		}
	}

}
